class KElement {
  int? id;
  late String name;
  late String tag;
  String? color;
  Map<String, dynamic>? attributes;
  List<KElement>? children;
  KElement? child;
  KElement? appBar;
  late bool isShow;

  KElement({
    this.id,
    required this.name,
    required this.tag,
    this.attributes,
    this.children,
    this.child,
    this.appBar,
    this.isShow = true,
    this.color,
  });

  KElement.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    tag = json['tag'];
    attributes = json['attributes'];
    // 修复 children 字段
    if (json['children'] is List) {
      children = (json['children'] as List)
          .map((e) => e != null ? KElement.fromJson(e) : null)
          .whereType<KElement>()
          .toList();
    }
    // 修复 child 字段
    child = json['child'] != null ? KElement.fromJson(json['child']) : null;
    // 修复 appBar 字段
    appBar = json['appBar'] != null ? KElement.fromJson(json['appBar']) : null;
    isShow = json['isShow'] ?? true; // 默认值为 true
    color = json['color'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = this.id;
    data['name'] = this.name;
    data['tag'] = this.tag;
    data['attributes'] = this.attributes;
    // 修复 children 字段
    data['children'] = this.children?.map((e) => e.toJson()).toList();
    // 修复 child 字段
    data['child'] = this.child?.toJson();
    // 修复 appBar 字段
    data['appBar'] = this.appBar?.toJson();
    data['isShow'] = this.isShow;
    data['color'] = this.color;
    return data;
  }
}
